package my.ts.code;

import java.util.ArrayList;

import my.ts.code.listener.ConfirmListener;
import my.ts.code.net.AppContext;
import my.ts.code.objects.Root;
import my.ts.code.util.Util;
import my.ts.code.viewgroup.ContentItem;
import my.ts.code.viewgroup.LotteryItem;
import my.ts.code.viewgroup.Paging;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ContentScreen extends BaseActivity implements ConfirmListener, OnClickListener {

	TextView title;
	LinearLayout content;
	ImageView image;
	Root root;
	ArrayList<String> sids = new ArrayList<String>();
	ArrayList<String> titles = new ArrayList<String>();
	int id, position = 0;
	Drawable d = null;
	private Context context;
	private Button left, right;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		context = this;
		setCListener(this);
		setContentView(R.layout.content_screen);
		init();

		showDialogLoading(this, "Loading Data");

		getData(id, sids.get(position));
	}

	@Override
	public void onResume() {

		super.onResume();
		AppContext.instance.session.setListener(this);

	}

	public void getData(int id, String sid) {
		switch (id) {
		case Config.TIN_TUC:
			AppContext.instance.session.getContentNews(sid);
			break;

		case Config.TRUYEN:
			AppContext.instance.session.getContentStory(sid);
			break;

		case Config.CURRENCY:
			if (sid.equals("0"))
				AppContext.instance.session.getCurrency();
			else if (sid.equals("1"))
				AppContext.instance.session.getGoldPrice();
			break;

		case Config.LOTTERY:
			AppContext.instance.session.getLotteryResult(sid);
			break;

		default:
			break;
		}
	}

	public void init() {
		Bundle b = getIntent().getExtras();
		sids = b.getStringArrayList("sid");
		id = b.getInt("id");
		position = b.getInt("pos");
		
		titles = b.getStringArrayList("title");

		title = (TextView) findViewById(R.id.title);
		title.setText(titles.get(position));
		
		left = (Button) findViewById(R.id.left);
		left.setOnClickListener(this);
		right = (Button) findViewById(R.id.right);
		right.setOnClickListener(this);
		
		if (position == 0)
			left.setVisibility(View.GONE);
		
		if (position == sids.size() - 1)
			right.setVisibility(View.GONE);

		content = (LinearLayout) findViewById(R.id.contents);
		image = (ImageView) findViewById(R.id.image);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {
				if (root.getMessage() != null) {
					showDialogConfirmSMS(context, ContentScreen.this,
							root.getMessage(), "Thông báo",
							Config.CHARGE_7_SYNTAX.getSyntax() + "15000 "
									+ Config.TSM,
							Config.CHARGE_7_SYNTAX.getNumber(), false, true);
				} else {

					int size = root.getItems().size();

					if (id == Config.LOTTERY) {
						for (int i = 0; i < size; i++) {
							Paging p = new Paging(context, "Ngày mở thưởng "
									+ root.getItems().get(i).getOpenDate());
							content.addView(p);

							LotteryItem i1 = new LotteryItem(context,
									"Giải đặc biệt:", root.getItems().get(i)
											.getSpecial());
							content.addView(i1);
							
							LotteryItem i2 = new LotteryItem(context,
									"Giải nhất:", root.getItems().get(i)
											.getFirst());
							content.addView(i2);

							LotteryItem i3 = new LotteryItem(context,
									"Giải nhì:", root.getItems().get(i)
											.getSecond());
							content.addView(i3);

							LotteryItem i4 = new LotteryItem(context,
									"Giải tư:", root.getItems().get(i)
											.getFourth());
							content.addView(i4);

							LotteryItem i5 = new LotteryItem(context,
									"Giải năm:", root.getItems().get(i)
											.getFifth());
							content.addView(i5);

							LotteryItem i6 = new LotteryItem(context,
									"Giải sáu:", root.getItems().get(i)
											.getSixth());
							content.addView(i6);

							LotteryItem i7 = new LotteryItem(context,
									"Giải bảy:", root.getItems().get(i)
											.getSeventh());
							content.addView(i7);

//							LotteryItem i8 = new LotteryItem(context,
//									"Giải đặc biệt:", root.getItems().get(i)
//											.getSpecial());
//							content.addView(i1);

							
						}
					} else
						for (int i = 0; i < size; i++) {
							ContentItem di = new ContentItem(getBaseContext(),
									root.getItems().get(i));
							content.addView(di);
							// content.setText(root.getItems().get(0).getContent());
						}
				}
			} else if (msg.what == 2) {
				image.setImageDrawable(d);
			}
		}
	};

	@Override
	public void dispatch(String data) {
		// TODO Auto-generated method stub
		// System.out.println("ON DISPATCH");

		dialog.dismiss();

		root = parse(data);

		if (root != null) {
			Message msg = handler.obtainMessage(1);
			handler.sendMessage(msg);
			if (id != Config.LOTTERY)
				getImage(root.getItems().get(0).getImageURL());
		}
	}

	private void getImage(final String link) {
		Thread t = new Thread() {
			public void run() {
				d = Util.getImage(link);
				if (d != null) {
					Message msg = handler.obtainMessage(2);
					handler.sendMessage(msg);
				}
			}
		};
		t.start();
	}

	@Override
	public void confirmListener(boolean done) {
		// TODO Auto-generated method stub
		if (done) {
			showDialogLoading(context, "Loading data");
			Thread t = new Thread() {
				public void run() {
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					getData(id, sids.get(position));
				}
			};

			t.start();
		} else
			finish();

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.left:
			Intent intent = new Intent(ContentScreen.this,
					ContentScreen.class);
			Bundle b = new Bundle();
			b.putInt("id", id);
			b.putInt("pos", position - 1);
			
			b.putStringArrayList("sid", sids);
			b.putStringArrayList("title", titles);

			startActivity(intent.putExtras(b));
			finish();
			break;

		case R.id.right:
			Intent intentr = new Intent(ContentScreen.this,
					ContentScreen.class);
			Bundle br = new Bundle();
			br.putInt("id", id);
			br.putInt("pos", position + 1);
			
			br.putStringArrayList("sid", sids);
			br.putStringArrayList("title", titles);

			startActivity(intentr.putExtras(br));
			finish();
			break;

		default:
			break;
		}
		
	}
}
